package com.tingyuge.base.arithmetic.leetcode;

public class Test2 {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int temp = 0;
        ListNode resultNode = new ListNode(0);
        ListNode i3 = resultNode;
        while (l1 != null || l2 != null || temp!=0) {
            int l1Val = l1 !=null ? l1.val: 0;
            int l2Val = l2 !=null ? l2.val: 0;
            //进位
            int sumVal = l1Val+l2Val+ temp;
            temp =sumVal /10;
            ListNode cur = new ListNode(sumVal%10);
            i3.next=cur;
            i3= i3.next;
            if(l1 !=null){
                l1 = l1.next;
            }
            if(l2 !=null){
                l2 = l2.next;
            }
            System.out.println(resultNode.toString());
        }
        return resultNode.next;



    };

    public static void main(String[] args) {
        Test2 test2 = new Test2();
        ListNode listNode = new ListNode(1,new ListNode(3,new ListNode(4)));
        ListNode listNode2 = new ListNode(3,new ListNode(2,new ListNode(5)));

        ListNode listNode1 = test2.addTwoNumbers(listNode, listNode2);
        while (listNode1 !=null){
            System.out.println(listNode1.val);
            listNode1=listNode1.next;
        }
    }
    public static class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }
}


